package com.ooo.ttt.demo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import com.ooo.ttt.demo.utils.Constant;
import com.ooo.ttt.opencvlib.FaceDector;
import com.ooo.ttt.opencvlib.NativeFace;
import com.ooo.ttt.opencvlib.camera.CameraLoader;
import com.ooo.ttt.opencvlib.camera.OnFrameDataListener;
// Exception locking surface  java.lang.IllegalArgumentException
//

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable, FaceDector.OnNativeFaceResult {

    private Paint paint;

    //    private Thread mThread;
    private CameraLoader cameraLoader;
    private FaceDector faceDector;
    private NativeFace[] faces;
    private SurfaceHolder mHolder;

    public MySurfaceView(Context context) {
        this(context, null);
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(2f);
        this.getHolder().addCallback(this);

    }

    public void setCamera(CameraLoader cameraLoader) {

        this.cameraLoader = cameraLoader;
        this.cameraLoader.initCamera(getContext());
        faceDector = new FaceDector(Constant.INSTANCE.getDIR_PARTH() + "/lbpcascade_frontalface.xml", 0, this);


        this.faceDector.setCameraId(this.cameraLoader.getCameraId());
        this.cameraLoader.setOnFrameDataListener(new OnFrameDataListener() {
            @Override
            public void onPreviewFrame(byte[] data, int width, int height, int degree) {
                faceDector.detector(data, width, height, degree);
            }
        });
//        mThread = new Thread(this);

    }

    @Override
    public void onResult(NativeFace[] nativeFaces) {
        this.faces = nativeFaces;
        if (result != null) {
            result.onReuslt(nativeFaces);
        }
    }

    public NativeFace[] getFaces() {
        return faces;
    }

    public void setFaces(NativeFace[] faces) {
        this.faces = faces;
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        this.cameraLoader.setSurface(holder);
        this.faceDector.start();
        //  mStopThread = true;
//        mThread.start();
        mHolder = holder;

    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        Log.e("tagSurfaceView", width + "  " + height);// 1080  1572
        faceDector.setHeight(height);
        faceDector.setWidth(width);
        this.cameraLoader.startPreview();
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        this.cameraLoader.stopCamera();
        faceDector.stop();
        //   stopTimer();
    }


    public interface FaceRectResult {
        void onReuslt(NativeFace[] nativeFaces);
    }


    public FaceRectResult result;

    public void setResult(FaceRectResult result) {
        this.result = result;
    }

    @Override
    public void run() {
//        do {
//            synchronized (this) {
//                draw(getFaces());
//            }
//        } while (mStopThread);
    }


//    private void draw(NativeFace[] faces) {
//        if (faces == null || faces.length == 0) {
//            return;
//        }
//        if (mHolder == null) {
//            return;
//        }
//        try {
//
//            Canvas canvas = mHolder.lockCanvas();
//            Log.e("tagCanvas", (canvas == null) + "");// true
//            if (canvas != null) {
//                for (int i = 0; i < faces.length; i++) {
//                    NativeFace face = faces[i];
//
//                    drawRect(face.x, face.y, face.width, face.height, canvas);
//                }
//                mHolder.unlockCanvasAndPost(canvas);
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
//
//
//    private void drawRect(int x, int y, int width, int height, Canvas canvas) {
//        int left = x;
//        int top = y;
//        int right = x + width;
//        int bottom = y + height;
//
//        if (canvas != null) {
//            canvas.drawRect(left, top, right, bottom, paint);
//        }
//    }
//
//
//    public NativeFace[] getFaces() {
//        return faces;
//    }
//
//
//    public void stopTimer() {
//        mStopThread = false;
//    }
//
//
//    private boolean mStopThread = true;

}
